/*
 * Copyright (C) 2020 xuexiangjys(xuexiangjys@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.now.printer.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.flyco.tablayout.SlidingTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.now.printer.R;
import com.now.printer.base.BaseActivity2;
import com.now.printer.db.DocumentsTable;
import com.now.printer.ui.fragment.SimpleCardFragment;
import com.now.printer.utils.DbUtils;
import com.now.printer.utils.FileManager;
import com.now.printer.utils.FileUtils;
import com.now.printer.utils.ResUtils;
import com.now.printer.utils.Utils;

import org.xutils.DbManager;
import org.xutils.common.util.LogUtil;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;


/**
 * 搜索页面
 *
 * @author xuexiang
 * @since 2020/4/1 1:26 AM
 */
public class FileBrowerManagerActivity extends BaseActivity2 {
    private RelativeLayout rlTop;
    private ImageView imgToBack;
    private ImageView imgHelp;
    private EditText etSearch;
    private TextView tvBtnSearch;
    private TextView tvTitle;

    //    private SearchListAdapter searchListAdapter;
    private String keyStore = "";
    private SlidingTabLayout tabLayout_1;
    private String type = "0";//0是所有，1是文件浏览，2是图片
    ArrayList<Fragment> fragments = new ArrayList<>();

    private static final String BUNDLE_KEY_FILE_TYPE = "BUNDLE_KEY_FILE_TYPE";
    private static final String BUNDLE_KEY_KEY_WORD = "keyWord";

    String[] mSearchType;
    ViewPager vp;

    @Override
    public int getLayoutId() {
        return R.layout.activity_search_component;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void doBusiness(Context mContext) {
        initView();
        initData();
    }


    public void initView() {

        tvTitle = (TextView) findViewById(R.id.tv_title);
        rlTop = findViewById(R.id.rl_top);
        imgToBack = (ImageView) findViewById(R.id.img_toBack);
        imgHelp = (ImageView) findViewById(R.id.imgHelp);
        imgHelp.setOnClickListener(onClickListener);
        etSearch = (EditText) findViewById(R.id.etSearch);
        tvBtnSearch = (TextView) findViewById(R.id.tvBtnSearch);

        tabLayout_1 = findViewById(R.id.tl_1);
        vp = findViewById(R.id.vp);
        tvBtnSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                EventBus.getDefault().post(MessageWrap.getInstance(vp.getCurrentItem(),etSearch.getText().toString().trim()));
                Bundle bundle = new Bundle();
                bundle.putInt(BUNDLE_KEY_FILE_TYPE, vp.getCurrentItem());
                bundle.putString(BUNDLE_KEY_KEY_WORD, etSearch.getText().toString().trim());
                fragments.get(vp.getCurrentItem()).setArguments(bundle);
                fragments.get(vp.getCurrentItem()).onAttach(getApplicationContext());
            }
        });
        imgToBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    private void initData() {
        mSearchType = ResUtils.getStringArray(R.array.search_type);
        type = getIntent().getStringExtra("type");
        if ("0".equals(type)) {
            tvTitle.setText("搜索");
            for (int i = 0; i < mSearchType.length; i++) {
                fragments.add(SimpleCardFragment.getInstance(i, etSearch.getText().toString().trim()));
            }
        } else if ("1".equals(type)) {
            tvTitle.setText("所有文件");
            for (int i = 0; i < mSearchType.length - 1; i++) {
                fragments.add(SimpleCardFragment.getInstance(i, etSearch.getText().toString().trim()));
            }
        } else if ("2".equals(type)) {
            tvTitle.setText("图库");
            tabLayout_1.setVisibility(View.GONE);
            fragments.add(SimpleCardFragment.getInstance(FileUtils.TYPE_PIC, etSearch.getText().toString().trim()));
        }
        vp.setOffscreenPageLimit(mSearchType.length);
        tabLayout_1.setViewPager(vp, mSearchType, this, fragments);
        vp.setCurrentItem(0);
        tabLayout_1.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                vp.setCurrentItem(position);

                }

            @Override
            public void onTabReselect(int position) {
            }
        });
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {
                Bundle bundle = new Bundle();
                bundle.putInt(BUNDLE_KEY_FILE_TYPE, vp.getCurrentItem());
                bundle.putString(BUNDLE_KEY_KEY_WORD, etSearch.getText().toString().trim());
                fragments.get(vp.getCurrentItem()).setArguments(bundle);
                fragments.get(vp.getCurrentItem()).onAttach(getApplicationContext());

            }
        });

    }

    @Override
    public void onBackPressed() {
        if (this.getSupportFragmentManager().getBackStackEntryCount() == 1) {
            Utils.syncMainPageStatus();
        }
        super.onBackPressed();
    }

}


